/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/14
 * Time: 18:08
 */
public class Solution {
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        for(int cur : nums){
            sum +=  cur;
        }
        //如果x比整个数组的和还要大
        if(sum < x){
            return -1;
        }
        //转换为->求子数组的和等于target,求子数组的最大长度
        int target = sum - x;
        int left = 0;
        int right = 0;
        int count = Integer.MIN_VALUE;
        sum = 0;
        while(right < nums.length){
            sum += nums[right];
            while (sum > target){
                sum -= nums[left++];
            }
            if(sum == target){
                count =  Math.max(count, right - left + 1);
            }
            right++;
        }
        return count != Integer.MIN_VALUE ? (nums.length - count) : -1;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        int ret = s.minOperations(new int[]{1, 2, 3, 4, 5}, 6);
        System.out.println(ret);
    }
}
